﻿(function (window) {
    function GoldDigger(sum,bonus) {
        this.initialize(sum, bonus);
    };
    
    var p = GoldDigger.prototype = new createjs.Container();
    p.Container_initialize = p.initialize;
    var containerStart;
    var containerPlay;
    var label11;
    var label22;
    var that;
    var countTick ;
    var sumBonus;
    var bonusGame;
    var turnId = 0;
    p.initialize = function (sum,bonus) {
        this.Container_initialize();
        
        that = this;
        raiseGold = undefined;
        turnId = 0;
        sumBonus = sum;
        bonusGame = bonus;
        containerStart = new createjs.Container();
        containerPlay = new createjs.Container();

        countTick = 10;
       
        var msg = new createjs.Text("Bấm vào để chơi tiếp...", "25px Comic Sans", "#FFFFFF");
        var tile = new createjs.Shape();
        tile.graphics.beginFill("#00F");
        tile.graphics.drawRect(0, 0, 589, 306);
        tile.graphics.endFill();
        tile.alpha = 0.01;

        msg.x = 200;
        msg.y = 280;
        var bit = new createjs.Bitmap(preload.getResult("g9"));
        bit.scaleX = .7;
        bit.scaleY = .7;
        bit.y = -35;
        bit.x = 75;
        containerStart.addChild(tile, bit, msg);
        containerStart.addEventListener("click", initMini);
        this.addChild(containerStart, containerPlay);
        
        
    };

    function initMini() {
        containerPlay.alpha = 0;
        containerStart.removeAllChildren();

        var label = new createjs.Bitmap(preload.getResult("labelText"));
        label.x = 42;
        label.y = 62;
        
        label11 = new createjs.Text(countTick, "bold 35px Impact", "#FFFFFF");
        label11.x = 80;
        label11.y = 106;
        
        label22 = new createjs.Text("x"+bonusGame.DigGoldInfo[0].Multiplier, "bold 35px Impact", "#FFFFFF");
        label22.x = 82;
        label22.y = 249;

        for (var i = 0; i < 6; i++) {
            for (var j = 0; j < 5; j++) {
                var dig = new DiggerObj(i);
                dig.x = 165 + 29.5 + i * (59 + 6);
                dig.y = 10 + 28.5 + j * (57 + 4);
                dig.addEventListener("click", onClick);
                containerPlay.addChild(dig);
            }
        }
        containerPlay.addChild(label, label11, label22);
        createjs.Tween.get(containerPlay, { loop: false }).to({ alpha: 1 }, 1000);

         
    }

    function onClick(targ) {
        if (countTick <= 0)
            return;
        var r = targ.target.parent;
        r.removeAllEventListeners();
        var obj = bonusGame.DigGoldInfo[turnId];
        
        r.onEat(obj.ItemID, obj.EarnMoney);

        if (obj.ItemID != 1) {
            countTick--;
            label11.text = countTick;
            if (countTick == 0) {
               
                createjs.Tween.get(that, { loop: false }).wait(1000).call(function() {
                    var bggold = new createjs.Bitmap(preload.getResult("bggold"));
                    var b = bggold.getBounds();
                    bggold.x = (589 - b.width) / 2;
                    bggold.y = (306 - b.height) / 2;
                    var tt = new createjs.Text(PortalCommon.formatMoney(sumBonus), "bold 30px Impact", "#FFFFFF");
                    bggold.addEventListener('click', function() {
                        raiseGold();
                    });
                    PortalCommon.UpdateTotalWin(sumBonus);
                    var b1 = tt.getBounds();
                    tt.x = (589 - b1.width) / 2;
                    tt.y = (306 - b1.height) / 2-20;
                    containerPlay.addChild(bggold, tt);

                });

            }
        } else {
            label22.text = "x"+obj.Multiplier;
        }
        turnId++;
        
    }

    var raiseGold;
    p.OnRaise = function (callback) {

        raiseGold = callback;
        
    };
    
    
    window.GoldDigger = GoldDigger;
}(window));

 